package com.siam.package_rider.feign;

import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_rider.entity.TbDeliveryOrder;
import com.siam.package_rider.enums.DeliveryOrderStatusEnum;
import com.siam.package_rider.model.param.TbDeliveryOrderParam;
import com.siam.package_rider.service.TbDeliveryOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.siam.package_common.constant.RedisConstant.ORDER_START_ADDRESS_KEY;

/**
 * ---------------------------
 * 配送订单表 (TbDeliveryOrderController)         
 * ---------------------------
 * 作者：  高品
 * 时间：  2025-03-29 18:55:18
 * ---------------------------
 */
@RestController
public class DeliveryOrderFeignProvider implements DeliveryOrderFeignApi{

	@Autowired
	private TbDeliveryOrderService tbDeliveryOrderService;

	@Autowired
	private RedisTemplate<String,Object> redisTemplate;

	/**
	 * 保存配送订单表
	 * @param param
	 * @return
	 */
	public BasicResult insert(@RequestBody TbDeliveryOrderParam param) {
		tbDeliveryOrderService.insert(param);
		return BasicResult.success();
	}

	/**
	 * 删除配送订单表
	 * @param id
	 * @return
	 */
	public BasicResult delete(@RequestParam Long id) {
		tbDeliveryOrderService.delete(id);
		return BasicResult.success();
	}

	/**
	 * 批量删除配送订单表
	 * @param idList
	 * @return
	 */
	public BasicResult batchDelete(@RequestBody List<Long> idList) {
		tbDeliveryOrderService.batchDelete(idList);
		return BasicResult.success();
	}

	/**
	 * 修改配送订单表
	 * @param param
	 * @return
	 */
	public BasicResult updateById(@RequestBody TbDeliveryOrder param) {
		tbDeliveryOrderService.updateById(param);
		return BasicResult.success();
	}


	/**
	 * 根据主键查询
	 * @param id
	 * @return
	 */
	public BasicResult<TbDeliveryOrder> findById(@RequestParam Long id) {
		return BasicResult.success(tbDeliveryOrderService.findById(id));
	}

	@Override
	public BasicResult<TbDeliveryOrder> selectByOriginOrderId(int originOrderId) {
		return BasicResult.success(tbDeliveryOrderService.selectByOriginOrderId(originOrderId));
	}

	@Override
	public BasicResult updateOrderByCancel(Long id) {
		// 修改状态为已取消
		TbDeliveryOrder updateDeliveryOrder = new TbDeliveryOrder();
		updateDeliveryOrder.setId(id);
		updateDeliveryOrder.setStatus(Quantity.INT_6);
		updateDeliveryOrder.setCancelTime(new Date());
		updateDeliveryOrder.setUpdateTime(new Date());
		tbDeliveryOrderService.updateById(updateDeliveryOrder);

		// 移除redis里面的待抢单数据
		redisTemplate.opsForGeo().remove(ORDER_START_ADDRESS_KEY, id.toString());

		return BasicResult.success();
	}
}